package com.hzvictory.hotel_system.service.Impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.hzvictory.hotel_system.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hzvictory.hotel_system.pojo.entity.Dishorderitem;
import com.hzvictory.hotel_system.mapper.DishorderMapper;
import com.hzvictory.hotel_system.pojo.entity.Dishorder;
import com.hzvictory.hotel_system.service.IDishorderService;

/**
 * 菜品订单管理Service业务层处理
 *
 * @author hzvictory
 * @date 2025-07-25
 */
@Service
public class DishorderServiceImpl implements IDishorderService
{
    @Autowired
    private DishorderMapper dishorderMapper;

    /**
     * 查询菜品订单管理
     *
     * @param id 菜品订单管理主键
     * @return 菜品订单管理
     */
    @Override
    public Dishorder selectDishorderById(Long id)
    {
        return dishorderMapper.selectDishorderById(id);
    }

    /**
     * 查询菜品订单管理列表
     *
     * @param dishorder 菜品订单管理
     * @return 菜品订单管理
     */
    @Override
    public List<Dishorder> selectDishorderList(Dishorder dishorder)
    {
        return dishorderMapper.selectDishorderList(dishorder);
    }
    /**
     * 根据用户id查询菜品订单管理列表
     *
     * @param userId 用户id
     * @return 菜品订单管理
     */
    @Override
    public List<Dishorder> selectDishorderListByUserId(Long userId)
    {
        return dishorderMapper.selectDishorderListByUserId(userId);
    }

    /**
     * 新增菜品订单管理
     *
     * @param dishorder 菜品订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDishorder(Dishorder dishorder)
    {
        int rows = dishorderMapper.insertDishorder(dishorder);
        insertDishorderitem(dishorder);
        return rows;
    }

    /**
     * 修改菜品订单管理
     *
     * @param dishorder 菜品订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDishorder(Dishorder dishorder)
    {
        dishorderMapper.deleteDishorderitemByOrderId(dishorder.getId());
        insertDishorderitem(dishorder);
        return dishorderMapper.updateDishorder(dishorder);
    }
    /**
     * 修改菜品订单状态 1待支付->2已支付
     *
     * @param id 菜品订单id
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDishorderStatusById(Long id)
    {
        return dishorderMapper.updateDishorderStatusById(id);
    }

    /**
     * 批量删除菜品订单管理
     *
     * @param ids 需要删除的菜品订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDishorderByIds(Long[] ids)
    {
        dishorderMapper.deleteDishorderitemByOrderIds(ids);
        return dishorderMapper.deleteDishorderByIds(ids);
    }

    /**
     * 删除菜品订单管理信息
     *
     * @param id 菜品订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDishorderById(Long id)
    {
        dishorderMapper.deleteDishorderitemByOrderId(id);
        return dishorderMapper.deleteDishorderById(id);
    }

    /**
     * 新增菜品订单详情信息
     *
     * @param dishorder 菜品订单管理对象
     */
    public void insertDishorderitem(Dishorder dishorder)
    {
        List<Dishorderitem> dishorderitemList = dishorder.getDishorderitemList();
        Long id = dishorder.getId();
        if (StringUtils.isNotNull(dishorderitemList))
        {
            List<Dishorderitem> list = new ArrayList<Dishorderitem>();
            for (Dishorderitem dishorderitem : dishorderitemList)
            {
                dishorderitem.setOrderId(id);
                list.add(dishorderitem);
            }
            if (list.size() > 0)
            {
                dishorderMapper.batchDishorderitem(list);
            }
        }
    }
}
